<?php
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\wiki\Wiki as dbWiki;
use Biz\service\wiki\AdmWiki;
use Doraemon\model\DevidePackage\PackageRelatedInfo;
use Doraemon\model\DevidePackage\PicPackage;

$wikiIds = [];
$wikiIds1 = getWikiIds1();
$wikiIds2 = getWikiIds2();
$wikiIds3 = getWikiIds3();
$wikiIds4 = getWikiIds4();
$wikiIds = array_merge($wikiIds1, $wikiIds2, $wikiIds3, $wikiIds4);
$wikiIds = array_unique($wikiIds);
var_dump('需要打包wiki数量:' . count($wikiIds));
package($wikiIds);
die;

function getWikiIds1()
{
//查询食品类wiki
    $ids = AdmWiki::admSearchContentByReason(3);
    $where = [
        'id' => $ids,
        'status[!]' => dbWiki::STATUS_DELETE,
        'addtime[<]' => date('Y-m-d', time() - 86400),
    ];
    $wikiIds = dbWiki::selectByWhere($where, 'id');
    var_dump('食品类wiki数量：' . count($wikiIds));
    return $wikiIds;
}

function getWikiIds2()
{
//查询宠物-物品类wiki
    $where = [
        'category_id' => 120,
        'second_category_id' => 121,
        'status[!]' => dbWiki::STATUS_DELETE,
        'addtime[<]' => date('Y-m-d', time() - 86400),
    ];
    $wikiIds = dbWiki::selectByWhere($where, 'id');
    var_dump('宠物-物品类wiki数量：' . count($wikiIds));
    return $wikiIds;
}

function getWikiIds3()
{
//查询健身运动类wiki
    $where = [
        'category_id' => 4,
        'second_category_id' => 23,
        'third_category_id' => 108,
        'status[!]' => dbWiki::STATUS_DELETE,
        'addtime[<]' => date('Y-m-d', time() - 86400),
    ];
    $wikiIds1 = dbWiki::selectByWhere($where, 'id');
    var_dump('###健身运动类(电器-生活电器-美容美体)wiki数量：' . count($wikiIds1));
    $where = [
        'AND' => [
            'OR' => [
                'AND #1' => [
                    'title[~]' => '健身',
                ],
                'AND #2' => [
                    'title[~]' => '瑜伽',
                ],
                'AND #3' => [
                    'title[~]' => '划船机',
                ],
            ],
            'status[!]' => dbWiki::STATUS_DELETE,
            'addtime[<]' => date('Y-m-d', time() - 86400),
        ]
    ];
    $wikiIds2 = dbWiki::selectByWhere($where, 'id');
    var_dump('###健身运动类(商品关键词)wiki数量：' . count($wikiIds2));
    $wikiIds = array_merge($wikiIds1, $wikiIds2);
    $wikiIds = array_unique($wikiIds);
    var_dump('健身运动类(商品关键词)wiki数量:' . count($wikiIds));
    return $wikiIds;
}

function getWikiIds4()
{
//查询图书类wiki
    $where = [
        'AND' => [
            'OR' => [
                'AND #1' => [
                    'title[~]' => '著',
                ],
                'AND #2' => [
                    'channel_shop_name[~]' => '图书',
                ],
                'AND #3' => [
                    'channel_shop_name[~]' => '出版',
                ],
                'AND #4' => [
                    'channel_shop_name[~]' => '当当网',
                ],
                'AND #5' => [
                    'channel_shop_name[~]' => '新花文轩',
                ],
                'AND #6' => [
                    'channel_shop_name[~]' => '中信',
                ],
                'AND #7' => [
                    'channel_shop_name[~]' => '书店',
                ],
            ],
            'status[!]' => dbWiki::STATUS_DELETE,
            'addtime[<]' => date('Y-m-d', time() - 86400),
        ]
    ];
    $wikiIds = dbWiki::selectByWhere($where, 'id');
    var_dump('图书类wiki数量：' . count($wikiIds));
    return $wikiIds;
}


function package($wikiIds)
{
    return false;
    $config_file = '/home/adm_rsync_dir/package_collocation_wiki.txt';
    //获取配置
    $config = [];
    if (file_exists($config_file)) {
        $config = json_decode(file_get_contents($config_file), true)[0];
    }
    $wikiNum = $config['wiki_num'];
    $preHour = $config['pre_hour'];
    $periodHour = $config['period_hour'];
    $wikiCounts = count($wikiIds);
    $i = 0;
    $j = 1;
    while ($i < $wikiCounts) {
        $wikiMidIds = array_slice($wikiIds, $i, $wikiNum);
        var_dump('第' . $j . '个包，数量：' . count($wikiMidIds));
        if ($wikiMidIds && is_array($wikiMidIds)) {
            //主表
            $packageInfo = [
                "package_status" => PicPackage::STATUS_UNCLAIMED,
                "period" => $periodHour ?: 24,
                "wiki_num" => count($wikiMidIds),
                "show_time" => date('Y-m-d H:i:s', (time() + $preHour * 3600)),
                "addtime" => date('Y-m-d H:i:s', time()),
                "package_type" => PicPackage::PACKAGE_TYPE_WIKI
            ];
            $packageId = PicPackage::addPackage($packageInfo);
            //关联表
            foreach ($wikiMidIds as $wikiId) {
                $data = [
                    "package_id" => $packageId,
                    "wiki_id" => $wikiId,
                    "addtime" => date('Y-m-d H:i:s', time())
                ];
                PackageRelatedInfo::addPackageRelation($data);
            }
            echo "package_id=" . $packageId . "的任务包已添加成功\n";
        }
        $i = $i + $wikiNum;
        $j++;
    }
}
